package com.android.GernerateKeySet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class FileReceiveThread extends Thread{
	Socket socket;
	String filename;
	
	public FileReceiveThread(String serverIP, int serverFilePort, String filename) {
		this.filename = filename;
		InetAddress serverAddr;
		try {
			serverAddr = InetAddress.getByName(serverIP);
			socket = new Socket(serverAddr,serverFilePort);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public void run(){
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(new File(filename));
			byte[] buf = new byte[1024];
			int i;
			while (true) {
				InputStream is = socket.getInputStream();
				i = is.read(buf);
				if (i == -1)
					break;
				System.out.println(i + "Bytes read from server ");

				fos.write(buf, 0, i);
				System.out.println(i + "Bytes write to file ");
			}
			fos.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
